package com.test.deviceinfo.executor;

import com.test.deviceinfo.util.AbstractServer;
import com.test.deviceinfo.util.LinuxServer;
import com.test.deviceinfo.util.WinServer;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Executor {
    public static void main(String[] args) throws Exception {
        System.out.println("--------------->执行者开始执行");

        //获取对应系统的Server
        AbstractServer server = getServer();

        //构建要保存的目录，获取上级目录
        String userDir = System.getProperty("user.dir");
//        Path targetPath = Paths.get(userDir).getParent();
//        System.out.println("---------->工作目录：" + userDir);
//        System.out.println("---------->保存目录：" + targetPath);

        String cpuSerial = server.getCPUSerial();
        System.out.println("---------->CPU:\n"+cpuSerial);
        String mainBoardSerial = server.getMainBoardSerial();
        System.out.println("---------->主板:\n"+mainBoardSerial);
        String macAddress = server.getMacByInetAddress();
        System.out.println("---------->mac:\n"+macAddress);


        FileWriter fw = new FileWriter(userDir + File.separator + "info.txt");
        BufferedWriter bw = new BufferedWriter(fw);

        bw.write(String.format("mac:%s",macAddress));
        bw.newLine();
        bw.write(String.format("mainBoard:%s",mainBoardSerial));
        bw.newLine();
        bw.newLine();
        bw.write(String.format("cpu:%s",cpuSerial));
        bw.newLine();
        bw.newLine();
        bw.write("end");
        bw.newLine();

        bw.flush();
        bw.close();
        fw.close();

    }

    public static AbstractServer getServer() {
        String osName = System.getProperty("os.name").toLowerCase();
        AbstractServer server;
        //根据不同操作系统类型选择不同的数据获取方法
        if (osName.startsWith("windows")) {
            server = new WinServer();
        } else if (osName.startsWith("linux")) {
            server = new LinuxServer();
        } else {
            server = new WinServer();
        }
        return server;
    }
}
